﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BingoClientModel;

namespace BingoServerCommon.Common
{
	public class Package
	{
		//! 消息头
		public EnumPackCommand msgid;
		//! 消息数据
		protected AutoBuffer m_Buffer = new AutoBuffer(1024 * 5);
		//! 读消息位置
		protected int m_iPosition;
	
		//!
		public Package(EnumPackCommand mainid)
		{
			msgid = mainid;
			m_iPosition = 0;
			Write((int)msgid);
		}

		//!
		public Package(byte[] buffer, int len)
		{

			m_Buffer.WriteBuffer(buffer, 0, len);
			m_iPosition = 0;

			msgid = (EnumPackCommand)ReadInt();
		}

		//!复制包
		public virtual Package Clone(byte[] bytes)
		{
			return null;
		}
		//!包体逻辑执行
		public virtual void Excute(Session session)
		{
			string str = ReadString();
			Console.WriteLine("执行包体逻辑~~~~:" + str);
		}
        public virtual void Excute(BaseRoom room ,Session sessione)
        {
            Console.WriteLine("[重载，客户端用] -- 执行包体逻辑:");
        }

		public ushort getLen()
		{
			ushort len = (ushort)m_Buffer.GetDataCount();
			return len;
		}

		public void Write(int value)
		{
			m_Buffer.Write(value);

		}

		public void Write(short value)
		{
			m_Buffer.Write(value);

		}
        public void Write(ushort value)
        {
            m_Buffer.Write(value);

        }


        public void Write(long value)
		{
			m_Buffer.Write((uint)(value >> 32));


			m_Buffer.Write((uint)value);

		}

		public void Write(string value)
		{
			if (string.IsNullOrEmpty(value))
				value = string.Empty;

			m_Buffer.Write(value);

		}

		public void Write(byte value)
		{
			m_Buffer.Write(value);

		}

		public void Write(byte[] value)
		{
			m_Buffer.Write(value);

		}

		public void Write(uint value)
		{
			m_Buffer.Write(value);

		}

		#region Read

		public int ReadInt()
		{
			m_iPosition += sizeof(int);
			return NetFunction.ntoh(BitConverter.ToInt32(m_Buffer.m_Buffer, m_iPosition - sizeof(int)));
		}

		public uint ReadUInt()
		{
			m_iPosition += sizeof(uint);
			return NetFunction.ntoh(BitConverter.ToUInt32(m_Buffer.m_Buffer, m_iPosition - sizeof(uint)));
		}

		public short ReadShort()
		{
			m_iPosition += sizeof(short);
			return NetFunction.ntoh(BitConverter.ToInt16(m_Buffer.m_Buffer, m_iPosition - sizeof(short)));
		}

		protected ushort ReadUShort()
		{
			m_iPosition += sizeof(ushort);
			return NetFunction.ntoh(BitConverter.ToUInt16(m_Buffer.m_Buffer, m_iPosition - sizeof(ushort)));
		}

		public long ReadLong()
		{
			uint hight = NetFunction.ntoh(BitConverter.ToUInt32(m_Buffer.m_Buffer, m_iPosition));
			m_iPosition += sizeof(int);
			uint low = NetFunction.ntoh(BitConverter.ToUInt32(m_Buffer.m_Buffer, m_iPosition));
			m_iPosition += sizeof(int);

			return ((long)hight << 32) + (long)low;
		}

		public string ReadString()
		{
			ushort length = ReadUShort();

			m_iPosition += length;
			return Encoding.UTF8.GetString(m_Buffer.m_Buffer, m_iPosition - length, length).Trim(new char[1]);
		}

		public int[] ReadIntArr(int len)
		{
			string str = this.ReadString();
			if (string.IsNullOrEmpty(str))
				return new int[len];
			else
				return str.Split('#').Select(i => int.Parse(i)).ToArray();
		}

		public float[] ReadFloatArr(int len)
		{
			string str = this.ReadString();
			if (string.IsNullOrEmpty(str))
				return new float[len];
			else
				return str.Split('#').Select(i => float.Parse(i)).ToArray();
		}

		public List<int> ReadIntLst()
		{
			string str = this.ReadString().Trim('#');
			if (string.IsNullOrEmpty(str))
				return new List<int>();
			else
				return str.Split('#').Select(i => int.Parse(i)).ToList();
		}



		public byte ReadByte()
		{
			m_iPosition += sizeof(byte);
			return m_Buffer.m_Buffer[m_iPosition - sizeof(byte)];
		}

		public byte[] ReadByteArray()
		{
			ushort length = ReadUShort();
			m_iPosition += length;

			byte[] arr = new byte[length];
			Array.Copy(arr, 0, m_Buffer.m_Buffer, m_iPosition - length, length);

			return arr;
		}

		#endregion

		public byte[] GetBuffer()
		{
			int len = m_Buffer.GetDataCount();
			byte[] retBuff = new byte[len];
			Array.Copy(m_Buffer.m_Buffer, 0, retBuff, 0, len);
			return retBuff;
		}

		//得到接收数据的16进制
		public string getHexStr()
		{
			byte[] bytes = this.GetBuffer();
			StringBuilder sb = new StringBuilder();
			if (bytes != null)
			{
				for (int i = 0; i < bytes.Length; i++)
				{
					sb.Append(bytes[i].ToString("X2"));
				}
			}
			return sb.ToString();
		}

	
	}
}